package com.orangelife.mobile.common.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.orangelife.mobile.goodsrent.dao.GoodsDao;
import com.orangelife.mobile.shoppingcart.bean.ShoppingCartBean;

public class DBHelper extends SQLiteOpenHelper {

    //	private static final String CREATE_CONVENIENCE = "create table tb_convince"
//			+ "(_id integer primary key autoincrement,icon VARCHAR,title VARCHAR)";
    private static DBHelper helper;
    private static Context APPLICATION_CONTEXT;
    private static final String TABLE_NAME1 = "tb_convince";
    public static final String DATABASE_NAME = "orangelife_resident.db3";
    public static final int DATABASE_VERSION = 4;
    /** 购物车表 */
    public static final String TB_SHOPPING_CART = "tb_shopping_cart";
    public static final String TB_GOODS_RENT = "tb_goods_rent";
    /** 创建周边表 */
    private static final String CREATE_TB_SHOPPING_CART = "CREATE TABLE IF NOT EXISTS " +
            TB_SHOPPING_CART + "("
            + "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
            + ShoppingCartBean.KEY_PRODUCT_ID + " text,"
            + ShoppingCartBean.KEY_NUM + " text,"
            // TODO: 2016-9-29 KEY_COMM_ID
            + ShoppingCartBean.KEY_COMM_ID + " text"
            + ");";
    /** 创建物品租借表 */
    private static final String CREATE_GOODS_RENT = "CREATE TABLE IF NOT EXISTS " +
            TB_GOODS_RENT + "(" +
            GoodsDao.ID + " integer primary key autoincrement," +
            GoodsDao.GOODS_ID + " text," +
            GoodsDao.GOODS_NAME + " text," +
            GoodsDao.GOODS_RENT_DAY + " text," +
            GoodsDao.GOODS_SIGLE_PRICE + " text," +
            GoodsDao.GOODS_UNIT_PRIDE + " text," +
            GoodsDao.GOODS_DISPATCH_TYPE + " text," +
            GoodsDao.GOODS_RENT_MAX_DAY + " text," +
            GoodsDao.GOODS_MER_ID + " text," +
            GoodsDao.GOODS_LOGO + " text" +
            ")";

    public static DBHelper getInstance() {
        if (helper == null) {
            helper = new DBHelper();
        }
        return helper;
    }

    public DBHelper() {
        super(APPLICATION_CONTEXT, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public static void init(Context context) {
        APPLICATION_CONTEXT = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TB_SHOPPING_CART);
        db.execSQL(CREATE_GOODS_RENT);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TB_SHOPPING_CART);
        db.execSQL("DROP TABLE IF EXISTS " + TB_GOODS_RENT);
        onCreate(db);
    }

}
